//#define _CRT_SECURE_NO_WARNINGS
//计算两个日期间间隔天数，先输入较早的日期
//可以英语全称、缩写输入月份，不区分大小写，或以数字输入月份
//报告所有用户输入的错误
//Days between two dates
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

typedef struct date {
	int year;
	char month[10];
	int day;
}date;

char sh[12][4]{ "JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC" };
char fu[12][10] = { "JANUARY", "FEBUARY", "MARCH", "APRIL", "MAY","JUNE",
"JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER", "DECEMBER" };
int ds[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

void gets_f(char* c, int size);
int years_cal(date* date1, date* date2);
int days1(date* d1, int mon);
int days2(date* d1, int mon);
int check(int check_mon, date* d);
int find_month(date* d);
int main()
{
	do
	{
		date date1;
		date date2;

		//input
		printf("Input two dates\n I will tell you how many days there are\n after the first and before the second\n");
		printf("***make sure the first date is earlier***\n");
		printf("input the year of the first date:\n");
		scanf("%d", &date1.year);
		getchar();
		printf("input the month of the first date(spell in full ,in short or input integer):\n");
		gets_f(date1.month, 10);
		printf("input the day of the first date\n");
		scanf("%d", &date1.day);
		getchar();
		printf("input the year of the second date\n");
		scanf("%d", &date2.year);
		getchar();
		printf("input the month of the second date(spell in full ,in short or input integer):\n");
		gets_f(date2.month, 10);
		printf("input the day of the second date\n");
		scanf("%d", &date2.day);
		getchar();
		//check
		int mon1 = find_month(&date1);
		int mon2 = find_month(&date2);
		int error1 = check(mon1, &date1);
		int error2 = check(mon2, &date2);
		if (date1.year > date2.year)
		{
			printf("Error:wrong year input,first later than the second.\n");
			error1 = 1;
		}
		else if (date1.year == date2.year && mon1 == mon2)
			if (date1.day > date2.day)
			{
				error1 = 1;
				printf("Error:wrong day input,first later than the second.\n");
			}
		if (error1 == 1)
			printf("Error in date1\n");
		if (error2 == 1)
			printf("Error in date2\n");
		if (error1 == 1 || error2 == 1)
			goto end;
		//calculate
		long int sum;
		sum = days1(&date1, mon1) + days2(&date2, mon2) + years_cal(&date1, &date2);
		//output
		printf("answer is %ld\n", sum);

	end:
		printf("Do you want to try again?(q to quit,others to continue):\n");
	} while (getchar() != 'q');
	printf("Bye!");
	return 0;
}


void gets_f(char* c, int size)
{
	int i = 0;
	fgets(c, size, stdin);
	while (c[i] != '\0')
	{
		if (islower(c[i]))
			c[i] = toupper(c[i]);
		if (c[i] == '\n')
		{
			c[i] = '\0';
			break;
		}
		i++;

	}
}
int years_cal(date* d1, date* d2)
{
	int i = 0, j = 0;
	int y1 = d1->year;
	int y2 = d2->year;
	if (y2 - y1 > 1)
	{
		for (i = 1; y1 + i < y2; i++)
			if (((y1 + i) % 4 == 0 && (y1 + i) % 100 != 0) || (y1 + i) % 400 == 0)
				j++;
		i--;
	}
	return 365 * i + j;
}

int days1(date* d1, int mon)
{
	int i;
	int total_d1 = 0;
	for (i = 11; i >= mon - 1; i--)
		total_d1 += ds[i - 1];
	total_d1 -= d1->day;
	if ((d1->year % 4 == 0 && d1->year % 100 != 0) || d1->year % 400 == 0)
		if (mon == 2 && d1->day != 29 || mon < 2)
			total_d1++;
	return total_d1;
}

int days2(date* d2, int mon)
{
	int i;
	int total_d2 = 0;
	for (i = 0; i <= mon - 2; i++)
		total_d2 += ds[i];
	total_d2 += d2->day;
	if ((d2->year % 4 == 0 && d2->year % 100 != 0) || d2->year % 400 == 0)
		if (mon == 2 && d2->day != 29 || mon > 2)
			total_d2++;
	return total_d2;
}

int find_month(date* d)
{
	int s_f = strlen(d->month);
	int mon = 0;
	int i, j = 1;
	if (isdigit(d->month[0]) && s_f <= 2)
	{
		for (i = 0; d->month[i] != '\0'; i++)
			;
		for (i --; i >= 0; i--, j *= 10)
			mon += ((int)(d->month[i]) - 48) * j;
		if (mon < 1 || mon>12)
			mon = 0;
	}
	if (isalpha(d->month[0]))
	{
		if (s_f < 4)
		{
			for (i = 0; i < 12; i++)
				if (!strcmp(d->month, sh[i]))
				{
					mon = i + 1;
					break;
				}
		}
		else
			for (i = 0; i < 12; i++)
				if (!strcmp(d->month, fu[i]))
				{
					mon = i + 1;
					break;
				}
	}
	return mon;
}
int check(int check_mon, date* d)
{
	int error = 0;

	if (check_mon && ds[check_mon - 1] < d->day)
	{
		if (!(((d->year % 4 == 0 && d->year % 100 != 0) || d->year % 400 == 0) && d->day == 29))
		{
			error = 1;
			printf("Error:wrong day input,too big.\n");
		}
	}
	if (!check_mon)
	{
		error = 1;
		printf("Error:wrong month input,check your spelling.\n");
	}
	if (d->year < 0)
		printf("Year Ok, but even Jesus was not even born then.\n");
	return error;
}